package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 自定义保存，同时保存套餐表，和套餐菜品关系表
     * @param setmealDto
     */

    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //先把套餐存入套餐表内
        this.save(setmealDto);

        //获取mp自动生成的套餐id
        Long id = setmealDto.getId();

        //再把套餐菜品关系存入套餐菜品关系表中

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //设置套餐id
        for(SetmealDish setmealDish : setmealDishes){
            setmealDish.setSetmealId(id);
        }

        //保存
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 自定义查询，同时查询套餐表以及套餐菜品关系表
     * @param id
     * @return
     */
    @Transactional
    @Override
    public SetmealDto getWithDish(Long id) {
        //现根据套餐id，查询套餐表的所有信息

        Setmeal setmeal = this.getById(id);

        //根据套餐id，查询套餐菜品关系表中所有对应菜品的信息

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        //创建setmealDto对象，并将setmeal，和list复制进去
        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal,setmealDto);

        setmealDto.setSetmealDishes(list);

        return setmealDto;


    }

    /**
     * 自定义，更新套餐表以及套餐菜品关系表
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {

        //先更新套餐表
        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDto,setmeal);

        this.updateById(setmeal);



        //先把这个套餐对应的菜品全删了
        Long setmealId = setmealDto.getId();

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);

        setmealDishService.remove(queryWrapper);

        //再重新添加新的
        //更新套餐菜品表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        for(SetmealDish setmealDish: setmealDishes){
            setmealDish.setSetmealId(setmealId);
        }

        setmealDishService.saveBatch(setmealDishes);

    }

    //单个删除或批量删除
    @Override
    public void deleteWithDish(List<String> ids) {

        //先删除套餐表中的数据
        this.removeByIds(ids);

        //再删除套餐菜品关系表
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(SetmealDish::getSetmealId,ids);

        setmealDishService.remove(queryWrapper);
    }
}
